package edu.sandau.f210153.f21010532.controller;


import edu.sandau.f210153.f21010532.model.Orderingmeals;
import edu.sandau.f210153.f21010532.repository.OrderingmealsRepository;
import edu.sandau.f210153.f21010532.service.OrderingmealsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

@Controller
public class OrderingmealsController {
    @Autowired
    private OrderingmealsRepository orderingmealsRepository;
    @Autowired
    private OrderingmealsService orderingmealService;
    @RequestMapping( "orderingmeal")
    public String getorderingmeal(ModelMap map, Orderingmeals orderingmeals) {
        if (StringUtils.isEmpty(orderingmeals.getOrderer())){
            orderingmeals.setOrderer(null);
        }
        if (StringUtils.isEmpty(orderingmeals.getMeals())) {
            orderingmeals.setMeals(null);
        }
        map.put("orderingmeals", orderingmealsRepository.findByOrdererOrMeals(orderingmeals.getOrderer(),orderingmeals.getMeals()));
        if (StringUtils.isEmpty(orderingmeals.getOrderer())&&StringUtils.isEmpty(orderingmeals.getMeals())){
            map.put("orderingmeals",orderingmealsRepository.findAll());
        }
        return "list";
    }

    @RequestMapping("orderingmeal/delete/{Id}")
    public String delete(@PathVariable Long Id){
        orderingmealService.delete(Id);
        return "redirect:/orderingmeal";
    }

    @RequestMapping("orderingmeal/create")
    public String create(){return "create";}

    @RequestMapping("orderingmeal/save")
    public String save(@RequestParam String meals,@RequestParam int price,@RequestParam String orderer,@RequestParam String status){
        edu.sandau.f210153.f21010532.model.Orderingmeals o = new edu.sandau.f210153.f21010532.model.Orderingmeals();
        o.setOrderer(orderer);
        o.setMeals(meals);
        o.setprice(price);
        o.setStatus(status);
        orderingmealService.save(o);
        return "redirect:/orderingmeal";
    }

    @RequestMapping("orderingmeal/update/{Id}")
    public String update(ModelMap map,@PathVariable Long Id){
        map.put("orderingmeal",orderingmealService.get(Id));
        delete(Id);
        return "update";
    }

    @RequestMapping("orderingmeal/edit")
    public String edit(edu.sandau.f210153.f21010532.model.Orderingmeals orderingmeal){
        orderingmealService.save(orderingmeal);
        return "redirect:/orderingmeal";
    }
}
